package com.boot.security.service.authentication;

import com.boot.security.model.bo.LoginAccountAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.ProviderNotFoundException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Service;

/**
 * @author 霜寒 <1621856595@qq.com>
 * @description 登录验证处理器
 * @date 2020/2/15 18:14
 **/

@Service
public class AccountAuthenticationProvider implements AuthenticationProvider {

    @Autowired
    AccountService service;

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        if (authentication instanceof LoginAccountAuthentication) {
            return service.authenticate((LoginAccountAuthentication) authentication);
        } else {
            throw new ProviderNotFoundException(String.format(" %s 仅支持 %s",
                    this.getClass().getSimpleName(), authentication.getClass().getSimpleName()));
        }
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return LoginAccountAuthentication.class.isAssignableFrom(authentication);
    }
}
